﻿using Senparc.Weixin.Cache;
using Senparc.Weixin.Cache.Redis;
using Senparc.Weixin.Threads;

namespace Qy.Yzrb
{
    public class SenparcWeiXinConfig
    {
        /// <summary>
        /// 注册 Senparc 微信 
        /// </summary>
        public static void Register()
        {
            RegisterWeixinCache();//注册分布式缓存
            RegisterWeixinThreads();//激活微信缓存（必须）
            RegisterSenparcQyWeixin();//注册Demo所用微信企业号的账号信息
        }

        /// <summary>
        /// 自定义缓存策略
        /// </summary>
        private static void RegisterWeixinCache()
        {

            //如果留空，默认为localhost（默认端口）
            var redisConfiguration = System.Configuration.ConfigurationManager.AppSettings["redisConnectString"];
            RedisManager.ConfigurationOption = redisConfiguration;

            // 如果不执行下面的注册过程，则默认使用本地缓存
            if (redisConfiguration != "Redis配置")
            {
                // RedisContainerCacheStrategy.Instance.CacheSetKey = "gdsfybjywxmp";
                CacheStrategyFactory.RegisterObjectCacheStrategy(() => RedisContainerCacheStrategy.Instance);//Redis
            }
            //CacheStrategyFactory.RegisterContainerCacheStrategy(() => MemcachedContainerStrategy.Instance);//Memcached
        }

        /// <summary>
        /// 激活微信缓存
        /// </summary>
        private static void RegisterWeixinThreads()
        {
            ThreadUtility.Register();
        }

        /// <summary>
        /// 注册Demo所用微信企业号的账号信息
        /// </summary>
        private static void RegisterSenparcQyWeixin()
        {
            Senparc.Weixin.QY.Containers.AccessTokenContainer.Register(
                System.Configuration.ConfigurationManager.AppSettings["qyCorpId"],
                System.Configuration.ConfigurationManager.AppSettings["qyCorpSecret"],
                "广医二-院长日报"
                );

            Senparc.Weixin.QY.Containers.ProviderTokenContainer.Register(
                System.Configuration.ConfigurationManager.AppSettings["qyCorpId"],
                System.Configuration.ConfigurationManager.AppSettings["qyCorpSecret"],
                "广医二-院长日报"
                );
        }
    }
}